/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.net.URLEncoder;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Document;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;

/**
 *
 * @author Nadia
 */
@WebService()
public class DocumentWebService {

    @PersistenceUnit
    EntityManagerFactory emf;
    private ArrayList objectArrayList = null;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "documentService")
    public Object[] documentService(@WebParam(name = "areaOfStudy") String areaOfStudy) throws Exception {
        String xpathExpression = "/document/areas_of_study/area[@name = '" + areaOfStudy + "']";
        objectArrayList = new ArrayList();
        Collection collection = emf.createEntityManager().createNamedQuery("Document.findAll").getResultList();
        for (Iterator it = collection.iterator(); it.hasNext();) {
            entities.Document object = (entities.Document) it.next();
            Document document = initDOM(object.getDocument());
            String result = (String) getXPathResult(document, xpathExpression);
            int intValue = Integer.valueOf(result).intValue();

            if (intValue > 0) {
                objectArrayList.add(URLEncoder.encode(object.getDocument(), "UTF-8"));
            }
        }
        Object array[] = objectArrayList.toArray();
        return array;
    }

    private Document initDOM(InputSource inputSource) throws Exception {

        Document xmlDOMReader = null;
        try {
            DocumentBuilderFactory dbf =
                    DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            if (inputSource != null) {
                xmlDOMReader = db.parse(inputSource);
            } else {
                xmlDOMReader = db.newDocument();
            }
            return xmlDOMReader;
        } catch (Exception ex) {
            throw ex;
        }
    }

    private Document initDOM(String xmlContent) throws Exception {
        return initDOM(xmlContent, false);
    }

    private Document initDOM(String xmlContent, boolean isXMLChunk) throws Exception {
        StringBuilder sb = new StringBuilder(xmlContent);
        if (isXMLChunk) {
            sb.insert(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><_wrappertag_ xmlns:ws='wrapper:wrapper'>");
            sb.append("</_wrappertag_>");
        }
        InputSource is = new InputSource(new java.io.CharArrayReader(sb.toString().toCharArray()));
        is.setEncoding("UTF-8");
        return initDOM(is);
    }

    private Object getXPathResult(Document doc, String xpathExpression) throws Exception {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        XPathExpression xPathExpression = xPath.compile(xpathExpression);
        Object result = xPathExpression.evaluate(doc);
        return result;
    }
}
